﻿using System;
using System.Collections.Generic;
using System.Text;
using DDBuildTools.DataAccess.Components;
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;

namespace DDBuildTools
{
    /// <summary>
    /// 生成FrameWork代码文件
    /// </summary>
    public class DDBuildFrameWork : IBuildFile
    {
        private string NameSpace;

        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="TableList">需要生成表</param>
        /// <param name="DataBaseCode">数据库名</param>
        /// <param name="TemplateUrl">模板路径</param>
        /// <param name="SavePath">保存路径</param>
        /// <returns></returns>
        public bool BuildFile(List<TableEntity> TableList, string DataBaseCode, string TemplateUrl, string SavePath)
        {
            NameSpace = Microsoft.VisualBasic.Interaction.InputBox("不能与其它项目生成相同", "请输入要生成程序的命名空间标识", "", 100, 100);   

            bool rBool = false;
            try
            {
                //装载模板文件
                string TemplateDir = AppDomain.CurrentDomain.BaseDirectory + Common.TemplateList[TemplateUrl];
                StringTemplateGroup group = new StringTemplateGroup("MyGroup", new LoadFileHtm(TemplateDir), typeof(DefaultTemplateLexer));
                BuildStoreProcedure(group, TableList, SavePath);
                BuildComponents(group, TableList, SavePath);
                BuildDataProvider(group, TableList, SavePath);
                rBool = true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("DDBuildFrameWork Error:"+ex.ToString(), "Alert");
            }
            return rBool;
        }

        #region "生成数据抽象类,Sql数据操作类,Access数据操作类,生成业务逻辑操作类"
        /// <summary>
        /// 生成数据抽象类,Sql数据操作类,Access数据操作类,生成业务逻辑操作类
        /// </summary>
        /// <param name="group"></param>
        /// <param name="TableList"></param>
        /// <param name="SavePath"></param>
        public void BuildDataProvider(StringTemplateGroup group, List<TableEntity> TableList, string SavePath)
        {
            //生成数据抽象类
            StringTemplate SqlTemplate = group.GetInstanceOf("FrameWork\\Data\\DataProvider");
            SqlTemplate.SetAttribute("NameSpace", NameSpace);
            SqlTemplate.SetAttribute("DateTime", DateTime.Now);
            List<TableEntityToNet> NewList = ConverToTableEntityToNet(TableList);
            SqlTemplate.SetAttribute("TableEntityToNets", NewList);
            Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\Data\\DataProvider.cs", SavePath));
            SqlTemplate.Attributes.Clear();
            //生成Sql数据操作类
            SqlTemplate = group.GetInstanceOf("FrameWork\\Data\\SqlDataProvider");
            SqlTemplate.SetAttribute("NameSpace", NameSpace);
            SqlTemplate.SetAttribute("DateTime", DateTime.Now);
            SqlTemplate.SetAttribute("TableEntityToNets", NewList);
            Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\Data\\SqlDataProvider.cs", SavePath));
            SqlTemplate.Attributes.Clear();
            //生成Access操作类
            SqlTemplate = group.GetInstanceOf("FrameWork\\Data\\AccessDataProvider");
            SqlTemplate.SetAttribute("NameSpace", NameSpace);
            SqlTemplate.SetAttribute("DateTime", DateTime.Now);
            SqlTemplate.SetAttribute("TableEntityToNets", NewList);
            Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\Data\\AccessDataProvider.cs", SavePath));
            SqlTemplate.Attributes.Clear();
            //生成Oracle操作类
            SqlTemplate = group.GetInstanceOf("FrameWork\\Data\\OracleDataProvider");
            SqlTemplate.SetAttribute("NameSpace", NameSpace);
            SqlTemplate.SetAttribute("DateTime", DateTime.Now);
            SqlTemplate.SetAttribute("TableEntityToNets", NewList);
            //Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\Data\\OracleDataProvider.cs", SavePath));
            SqlTemplate.Attributes.Clear();

            //生成业务逻辑操作类
            SqlTemplate = group.GetInstanceOf("FrameWork\\BusinessFacade");
            SqlTemplate.SetAttribute("NameSpace", NameSpace);
            SqlTemplate.SetAttribute("DateTime", DateTime.Now);
            SqlTemplate.SetAttribute("TableEntityToNets", NewList);
            Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\BusinessFacade"+NameSpace+".cs", SavePath));
            SqlTemplate.Attributes.Clear();

            BuildFrameWorkWeb(group, NewList, SavePath);
        }

        /// <summary>
        /// 生成FrameWorkWeb文件
        /// </summary>
        /// <param name="group"></param>
        /// <param name="TableList"></param>
        /// <param name="SavePath"></param>
        private void BuildFrameWorkWeb(StringTemplateGroup group, List<TableEntityToNet> TableList, string SavePath)
        {
            StringTemplate WebTemplate = group.GetInstanceOf("FrameWorkWeb\\Web");
            StringTemplate Manageraspx = group.GetInstanceOf("FrameWorkWeb\\Manageraspx");
            StringTemplate Manageraspxcs = group.GetInstanceOf("FrameWorkWeb\\Manageraspxcs");
            StringTemplate Defaultaspx = group.GetInstanceOf("FrameWorkWeb\\Defaultaspx");
            StringTemplate Defaultaspxcs = group.GetInstanceOf("FrameWorkWeb\\Defaultaspxcs");
            foreach (TableEntityToNet var in TableList)
            {
                //生成web.config文件
                WebTemplate.SetAttribute("DateTime", DateTime.Now);
                WebTemplate.SetAttribute("TableEntityToNet", var);
                Common.WriteFile(WebTemplate.ToString(), string.Format("{0}\\FrameWork.Web\\{1}\\Web.Config", SavePath, var.TableCode));
                WebTemplate.Attributes.Clear();
                //生成Default.aspx文件
                Defaultaspx.SetAttribute("NameSpace", NameSpace);
                Defaultaspx.SetAttribute("DateTime", DateTime.Now);
                Defaultaspx.SetAttribute("TableName", var.TableName);
                Defaultaspx.SetAttribute("TableCode", var.TableCode);
                Defaultaspx.SetAttribute("TableColumeToNets_Identity", var.TableColumeToNets_Identity);
                Defaultaspx.SetAttribute("TableColumeToNets_NoIdentity", var.TableColumeToNets_NoIdentity);
                Common.WriteFile(Defaultaspx.ToString(), string.Format("{0}\\FrameWork.Web\\{1}\\Default.aspx", SavePath, var.TableCode));
                Defaultaspx.Attributes.Clear();

                //生成Default.aspx.cs文件
                Defaultaspxcs.SetAttribute("NameSpace", NameSpace);
                Defaultaspxcs.SetAttribute("DateTime", DateTime.Now);
                Defaultaspxcs.SetAttribute("TableName", var.TableName);
                Defaultaspxcs.SetAttribute("TableCode", var.TableCode);
                Defaultaspxcs.SetAttribute("TableColumeToNets_Identity", var.TableColumeToNets_Identity);
                Defaultaspxcs.SetAttribute("TableColumeToNets_NoIdentity", var.TableColumeToNets_NoIdentity);
                Common.WriteFile(Defaultaspxcs.ToString(), string.Format("{0}\\FrameWork.Web\\{1}\\Default.aspx.cs", SavePath, var.TableCode));
                Defaultaspxcs.Attributes.Clear();

                //生成Manager.aspx文件
                Manageraspx.SetAttribute("NameSpace", NameSpace);
                Manageraspx.SetAttribute("DateTime", DateTime.Now);
                Manageraspx.SetAttribute("TableName", var.TableName);
                Manageraspx.SetAttribute("TableCode", var.TableCode);
                Manageraspx.SetAttribute("TableColumeToNets_Identity", var.TableColumeToNets_Identity);
                Manageraspx.SetAttribute("TableColumeToNets_NoIdentity", var.TableColumeToNets_NoIdentity);
                Common.WriteFile(Manageraspx.ToString(), string.Format("{0}\\FrameWork.Web\\{1}\\Manager.aspx", SavePath, var.TableCode));
                Manageraspx.Attributes.Clear();

                //生成Manager.aspx.cs文件
                Manageraspxcs.SetAttribute("NameSpace", NameSpace);
                Manageraspxcs.SetAttribute("DateTime", DateTime.Now);
                Manageraspxcs.SetAttribute("TableName", var.TableName);
                Manageraspxcs.SetAttribute("TableCode", var.TableCode);
                Manageraspxcs.SetAttribute("TableColumeToNets_Identity", var.TableColumeToNets_Identity);
                Manageraspxcs.SetAttribute("TableColumeToNets_NoIdentity", var.TableColumeToNets_NoIdentity);
                Common.WriteFile(Manageraspxcs.ToString(), string.Format("{0}\\FrameWork.Web\\{1}\\Manager.aspx.cs", SavePath, var.TableCode));
                Manageraspxcs.Attributes.Clear();
            }

        }

        /// <summary>
        /// 将TableEntity转为TableEntityToNet
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<TableEntityToNet> ConverToTableEntityToNet(List<TableEntity> list)
        {
            List<TableEntityToNet> NewList = new List<TableEntityToNet>();
            foreach (TableEntity var in list)
            {
                TableEntityToNet NewVar = new TableEntityToNet();
                NewVar.DataBaseCode = var.DataBaseCode;
                NewVar.TableCode = var.TableCode;
                NewVar.TableColume = var.TableColume;
                NewVar.TableName = var.TableName;
                NewVar.TableColumeToNets = ConvertToTableColumeToNet(var.TableColume);
                NewVar.ReturnType = GetIdentityType(NewVar.TableColumeToNets);
                NewVar.TableColumeToNets_Identity = GetTableColumeToNet_Identity(NewVar.TableColumeToNets);
                NewVar.TableColumeToNets_Key = GetTableColumeToNet_Key(NewVar.TableColumeToNets);
                NewVar.TableColumeToNets_NoIdentity = GetTableColumeToNet_NoIdentity(NewVar.TableColumeToNets);
                NewList.Add(NewVar);
            }
            return NewList;
        }

        /// <summary>
        /// 获得自动ID的.Net类型
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private string GetIdentityType(List<TableColumeToNet> List)
        {
            string rString = "";
            foreach (TableColumeToNet var in List)
            {
                if (var.Colume_Identity)
                {
                    rString = var.NetColume_Type;
                    break;
                }
            }
            return rString;
        }

        /// <summary>
        /// 获得自动ID列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToNet> GetTableColumeToNet_Identity(List<TableColumeToNet> List)
        {
            List<TableColumeToNet> l = new List<TableColumeToNet>();
            foreach (TableColumeToNet var in List)
            {
                if (var.Colume_Identity)
                {
                    l.Add(var);
                }
            }
            return l;
        }
        /// <summary>
        /// 获得主键列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToNet> GetTableColumeToNet_Key(List<TableColumeToNet> List)
        {
            List<TableColumeToNet> l = new List<TableColumeToNet>();
            foreach (TableColumeToNet var in List)
            {
                if (var.Colume_IsKey)
                {
                    l.Add(var);
                }
            }
            if (l.Count == 1)
            {
                if (l[0].Colume_Identity)
                    l.Clear();
            }
            return l;
        }
        /// <summary>
        /// 获得不包含自动ID列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToNet> GetTableColumeToNet_NoIdentity(List<TableColumeToNet> List)
        {
            List<TableColumeToNet> l = new List<TableColumeToNet>();
            foreach (TableColumeToNet var in List)
            {
                if (!var.Colume_Identity)
                {
                    l.Add(var);
                }
            }
            return l;
        }
        #endregion

        #region "生成实体类"
        /// <summary>
        /// 生成实体类
        /// </summary>
        /// <param name="group"></param>
        /// <param name="TableList"></param>
        /// <param name="SavePath"></param>
        public void BuildComponents(StringTemplateGroup group, List<TableEntity> TableList, string SavePath)
        {
            StringTemplate SqlTemplate = group.GetInstanceOf("FrameWork\\Components\\Components");
            foreach (TableEntity var in TableList)
            {
                SqlTemplate.SetAttribute("NameSpace", NameSpace);
                SqlTemplate.SetAttribute("DateTime", DateTime.Now);
                SqlTemplate.SetAttribute("TableName", var.TableName);
                SqlTemplate.SetAttribute("TableCode", var.TableCode);
                List<TableColumeToNet> list = ConvertToTableColumeToNet(var.TableColume);
                SqlTemplate.SetAttribute("TableColumeToNet", list);
                Common.WriteFile(SqlTemplate.ToString(), string.Format("{0}\\FrameWork\\Components\\{1}Entity.cs", SavePath, var.TableCode));
                SqlTemplate.Attributes.Clear();
            }
        }

        /// <summary>
        /// 将表结构转为.net数据对应列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToNet> ConvertToTableColumeToNet(List<TableColume> List)
        {
            List<TableColumeToNet> NewList = new List<TableColumeToNet>();
            foreach (TableColume var in List)
            {
                TableColumeToNet Tc = new TableColumeToNet();
                Tc.Colume_Code = var.Colume_Code;
                Tc.Colume_DefaultValue = var.Colume_DefaultValue;
                Tc.Colume_Identity = var.Colume_Identity;
                Tc.Colume_IsKey = var.Colume_IsKey;
                Tc.Colume_IsNullAble = var.Colume_IsNullAble;
                Tc.Colume_Length = var.Colume_Length;
                Tc.Colume_Name = var.Colume_Name;
                Tc.Colume_Scale = var.Colume_Scale;
                Tc.Colume_Type = var.Colume_Type;
                Tc.NetColume_DefaultValue = "";
                Tc.NetColume_SqlDbType = var.Colume_Type;
                Tc.NetColume_Type = var.Colume_Type;
                ConvertToColumeToNet(Tc);
                if (Tc.NetColume_Type == "String")
                {
                    Tc.NetColume_StringTrim = ".Trim()";
                }
                NewList.Add(Tc);
            }
            return NewList;
        }

        /// <summary>
        /// 转为.net列
        /// </summary>
        /// <param name="tc"></param>
        private void ConvertToColumeToNet(TableColumeToNet tc)
        {
            string DefaultType = tc.Colume_Type;
            string DefaultValue = "\"\"";
            string SqlDbType = "";
            string OleDbType = "";
            string OracleDbType = "";
            string ScriptCheckTitle = "";
            string ScriptCheckTitleSearch = "";
            string DefaultCsSink = "";
            string DefaultCsSearch = "";
            string DefaultCsSink_Data = string.Format("string {0}_Value = (string)Common.sink({0}.UniqueID, MethodType.Post, {1}, 0, DataType.Str);", tc.Colume_Code, tc.Colume_Length); 
            string DefaultCsSearch_String = @"
if ("+tc.Colume_Code+@"_Value != string.Empty)
{
    sb.AppendFormat("" AND "+tc.Colume_Code+@" like '%{0}%' "", Common.inSQL("+tc.Colume_Code+@"_Value));
}";
            string DefaultCsSearch_Int32 = @"
if ("+tc.Colume_Code+@"_Value != string.Empty)
{
    sb.AppendFormat("" AND " + tc.Colume_Code + @" = {0} "", Convert.ToInt32(" + tc.Colume_Code + @"_Value));
}";
            string ManagerCsSink = "";
            string ManagerCsSink_String = string.Format("string {0}_Value = (string)Common.sink({0}.UniqueID, MethodType.Post, {1}, {2}, DataType.Str);", tc.Colume_Code, tc.Colume_Length,tc.Colume_IsNullAble==true?0:1);
            string ManagerCsSink_Int = string.Format("int {0}_Value = (int)Common.sink({0}.UniqueID, MethodType.Post, {1}, {2}, DataType.Int);", tc.Colume_Code, tc.Colume_Length, tc.Colume_IsNullAble == true ? 0 : 1);
            string ManagerCsSink_Dat = string.Format("DateTime {0}_Value = (DateTime)Common.sink({0}.UniqueID, MethodType.Post, {1}, {2}, DataType.Dat);", tc.Colume_Code, 20, tc.Colume_IsNullAble == true ? 0 : 1);
            string ManagerCsSink_Long = string.Format("Int64 {0}_Value = (Int64)Common.sink({0}.UniqueID, MethodType.Post, {1}, {2}, DataType.Long);", tc.Colume_Code, tc.Colume_Length, tc.Colume_IsNullAble == true ? 0 : 1);
            string ManagerCsSink_Double = string.Format("double {0}_Value = (double)Common.sink({0}.UniqueID, MethodType.Post, {1}, {2}, DataType.Double);", tc.Colume_Code, tc.Colume_Length, tc.Colume_IsNullAble == true ? 0 : 1); 
            switch (tc.Colume_Type)
            {
                case "bit":
                    DefaultValue = "0";
                    DefaultType = "Boolean";
                    SqlDbType = "Bit";
                    OleDbType = "Boolean";
                    OracleDbType = "Int32";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~1f:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_Int32;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_Int;
                    break;
                case "int":
                    DefaultValue = "0";
                    DefaultType = "Int32";
                    SqlDbType = "Int";
                    OleDbType = "Integer";
                    OracleDbType = "Int32";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~2147483648:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_Int32;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_Int;
                    break;
                case "smallint":
                    DefaultValue = "0";
                    DefaultType = "Int16";
                    SqlDbType = "SmallInt";
                    OleDbType = "SmallInt";
                    OracleDbType = "Int16";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~32767:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_Int32;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_Int;
                    break;
                case "tinyint":
                    DefaultValue = "0";
                    DefaultType = "Byte";
                    SqlDbType = "TinyInt";
                    OleDbType = "TinyInt";
                    OracleDbType = "SByte";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~255:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_Int32;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_Int;
                    break;
                case "numeric":
                    if (tc.Colume_Scale != "0")
                    {
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                        ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                        ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                        ManagerCsSink = ManagerCsSink_Double;
                    }
                    else
                    {
                        DefaultValue = "0";
                        ScriptCheckTitle = string.Format("title=\"请输入{0}~9223372036854775807:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                        ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                        ManagerCsSink = ManagerCsSink_Long;
                    }
                    if (!tc.Colume_Identity)
                    {
                        if (tc.Colume_Scale != "0")
                        {
                            DefaultType = "Double";
                            
                        }
                        else {
                            DefaultType = "Decimal";
                        }
                        SqlDbType = "Decimal";
                        OleDbType = "Decimal";
                        OracleDbType = "Double";
                    }
                    else
                    {
                        DefaultType = "Int64";
                        SqlDbType = "BigInt";
                        OleDbType = "BigInt";
                        OracleDbType = "Number";
                    }
                    break;
                case "decimal":
                    if (tc.Colume_Scale != "0")
                    {
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                        ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                        ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                        ManagerCsSink = ManagerCsSink_Double;
                    }
                    else
                    {
                        DefaultValue = "0";
                        ScriptCheckTitle = string.Format("title=\"请输入{0}~9223372036854775807:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                        ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                        ManagerCsSink = ManagerCsSink_Long;
                    }
                    if (!tc.Colume_Identity)
                    {
                        if (tc.Colume_Scale != "0")
                        {
                            DefaultType = "Double";

                        }
                        else
                        {
                            DefaultType = "Decimal";
                        }
                        SqlDbType = "Decimal";
                        OleDbType = "Decimal";
                        OracleDbType = "Double";
                    }
                    else
                    {
                        DefaultType = "Int64";
                        SqlDbType = "BigInt";
                        OleDbType = "BigInt";
                        OracleDbType = "Number";
                    }
                    break;
                case "money":
                    if (tc.Colume_Scale != "0")
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                    else
                        DefaultValue = "0";
                    DefaultType = "Double";
                    SqlDbType = "Money";
                    OleDbType = "Currency";
                    OracleDbType = "Double";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Double;
                    break;
                case "smallmoney":
                    if (tc.Colume_Scale != "0")
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                    else
                        DefaultValue = "0";
                    DefaultType = "Double";
                    SqlDbType = "Money";
                    OleDbType = "Currency";
                    OracleDbType = "Double";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Double;
                    break;
                case "float":
                    if (tc.Colume_Scale != "0")
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                    else
                        DefaultValue = "0";
                    DefaultType = "Double";
                    SqlDbType = "Float";
                    OleDbType = "Currency";
                    OracleDbType = "Double";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Double;
                    break;
                case "real":
                    if (tc.Colume_Scale != "0")
                        DefaultValue = string.Format("0.{0}f", Common.BuildZero(Convert.ToInt32(tc.Colume_Scale)));
                    else
                        DefaultValue = "0";
                    DefaultType = "Single";
                    SqlDbType = "Real";
                    OleDbType = "Single";
                    OracleDbType = "Double";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~float{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Double;
                    break;
                case "datetime":
                    DefaultValue = "DateTime.Now";
                    DefaultType = "DateTime";
                    SqlDbType = "DateTime";
                    OleDbType = "Date";
                    OracleDbType = "DateTime";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~date{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Dat;
                    break;
                case "smalldatetime":
                    DefaultValue = "DateTime.Now";
                    DefaultType = "DateTime";
                    SqlDbType = "SmallDateTime";
                    OleDbType = "Date";
                    OracleDbType = "DateTime";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~date{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Dat;
                    break;
                case "char":
                    DefaultType = "String";
                    SqlDbType = "Char";
                    OleDbType = "Char";
                    OracleDbType = "Char";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "1");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "varchar":
                    DefaultType = "String";
                    SqlDbType = "VarChar";
                    OleDbType = "VarChar";
                    OracleDbType = "VarChar";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "text":
                    DefaultType = "String";
                    SqlDbType = "Text";
                    OleDbType = "LongVarChar";
                    OracleDbType = "LongVarChar";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "nchar":
                    DefaultType = "String";
                    SqlDbType = "NChar";
                    OleDbType = "WChar";
                    OracleDbType = "NChar";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "nvarchar":
                    DefaultType = "String";
                    SqlDbType = "NVarChar";
                    OleDbType = "VarWChar";
                    OracleDbType = "NVarChar";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "ntext":
                    DefaultType = "String";
                    SqlDbType = "NText";
                    OleDbType = "LongVarWChar";
                    OracleDbType = "LongVarChar";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~{1}:{2}\"", tc.Colume_Name, tc.Colume_Length, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    DefaultCsSearch = DefaultCsSearch_String;
                    DefaultCsSink = DefaultCsSink_Data;
                    ManagerCsSink = ManagerCsSink_String;
                    break;
                case "binary":
                    DefaultValue = "null";
                    DefaultType = "Byte[]";
                    SqlDbType = "Binary";
                    OleDbType = "Binary";
                    OracleDbType = "Raw";
                    break;
                case "varbinary":
                    DefaultValue = "null";
                    DefaultType = "Byte[]";
                    SqlDbType = "VarBinary";
                    OleDbType = "VarBinary";
                    OracleDbType = "Raw";
                    break;
                case "image":
                    DefaultValue = "null";
                    DefaultType = "Byte[]";
                    SqlDbType = "Image";
                    OleDbType = "LongVarBinary";
                    OracleDbType = "LongRaw";
                    break;
                case "bigint":
                    DefaultValue = "0";
                    DefaultType = "Int64";
                    SqlDbType = "BigInt";
                    OleDbType = "BigInt";
                    OracleDbType = "Number";
                    ScriptCheckTitle = string.Format("title=\"请输入{0}~9223372036854775807:int{1}\"", tc.Colume_Name, tc.Colume_IsNullAble == true ? "" : "!");
                    ScriptCheckTitleSearch = ScriptCheckTitle.Replace("!", "");
                    ManagerCsSink = ManagerCsSink_Long;
                    break;
            }
            tc.NetColume_Type = DefaultType;
            tc.NetColume_DefaultValue = DefaultValue;
            tc.NetColume_SqlDbType = SqlDbType;
            tc.NetColume_OleDbType = OleDbType;
            tc.NetColume_OracleDbType = OracleDbType;
            tc.ScriptCheckTitle = ScriptCheckTitle;
            tc.ScriptCheckTitleSearch = ScriptCheckTitleSearch;
            tc.DefaultCsSink = DefaultCsSink;
            tc.DefaultCsSearch = DefaultCsSearch;
            tc.ManagerCsSink = ManagerCsSink;
            if (tc.Colume_Identity)
            {
                tc.ManagerCsSink = string.Format("{0} IDX = ({0})Common.sink(\"IDX\", MethodType.Get, {1}, 0, DataType.{2});", tc.NetColume_Type, tc.Colume_Length, DefaultCsSearch == DefaultCsSearch_Int32 ? "Int" : "Long");
            }
            if (tc.NetColume_Type == "Byte[]")
            {
                tc.Netcolume_ConvertString = "(Byle[])";
            }
            else
            {
                tc.Netcolume_ConvertString = string.Format("Convert.To{0}", tc.NetColume_Type);
            }
        }
        #endregion

        #region "生成存储过程"
        /// <summary>
        /// 生成存储过程文档
        /// </summary>
        /// <param name="group"></param>
        /// <param name="SavePath"></param>
        /// <param name="TableList"></param>
        public void BuildStoreProcedure(StringTemplateGroup group, List<TableEntity> TableList, string SavePath)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            //生成Sql存储过程
            StringTemplate SqlTemplate = group.GetInstanceOf("Sql\\StoreProcedure");
            StringTemplate OracleTemplate = group.GetInstanceOf("Sql\\OracleSEQ");
            foreach (TableEntity var in TableList)
            {
                SqlTemplate.SetAttribute("NameSpace", NameSpace);
                SqlTemplate.SetAttribute("DateTime", DateTime.Now);
                SqlTemplate.SetAttribute("TableName", var.TableName);
                SqlTemplate.SetAttribute("TableCode", var.TableCode);
                List<TableColumeToSql> list = ConvertToTableColumeToSql(var.TableColume);
                SqlTemplate.SetAttribute("TableColumeToSql", list);
                SqlTemplate.SetAttribute("TableColumeToSql_NoIdentity", TableColumeToSql_NoIdentity(list));
                SqlTemplate.SetAttribute("TableColumeToSql_Identity", TableColumeToSql_Identity(list));
                SqlTemplate.SetAttribute("TableColumeToSql_Key", TableColumeToSql_Key(list));
                sb.Append(SqlTemplate.ToString());
                SqlTemplate.Attributes.Clear();

                OracleTemplate.SetAttribute("TableCode", var.TableCode.ToUpper());
                sb1.Append(OracleTemplate.ToString());
                OracleTemplate.Attributes.Clear();
            }
            Common.WriteFile(sb.ToString(), string.Format("{0}\\Sql\\StoreProcedure.sql", SavePath));
            //Common.WriteFile(sb1.ToString(), string.Format("{0}\\Sql\\OracleSEQ.sql", SavePath));
        }

        /// <summary>
        /// 只包含主键列(不包括只有一个主键的自动ID)
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToSql> TableColumeToSql_Key(List<TableColumeToSql> List)
        {
            List<TableColumeToSql> l = new List<TableColumeToSql>();
            foreach (TableColumeToSql var in List)
            {
                if (var.Colume_IsKey)
                {
                    l.Add(var);
                }
            }
            if (l.Count == 1)
            {
                if (l[0].Colume_Identity)
                    l.Clear();
            }
            return l;
        }

        /// <summary>
        /// 只包含ID列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToSql> TableColumeToSql_Identity(List<TableColumeToSql> List)
        {
            List<TableColumeToSql> l = new List<TableColumeToSql>();
            foreach (TableColumeToSql var in List)
            {
                if (var.Colume_Identity)
                {
                    l.Add(var);
                }
            }
            return l;
        }

        /// <summary>
        /// 不包含自动ID列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToSql> TableColumeToSql_NoIdentity(List<TableColumeToSql> List)
        {
            List<TableColumeToSql> l = new List<TableColumeToSql>();
            foreach (TableColumeToSql var in List)
            {
                if (!var.Colume_Identity)
                {
                    l.Add(var);
                }
            }
            return l;
        }

        /// <summary>
        /// 所有列
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        private List<TableColumeToSql> ConvertToTableColumeToSql(List<TableColume> List)
        {
            List<TableColumeToSql> NewList = new List<TableColumeToSql>();
            foreach (TableColume var in List)
            {

                TableColumeToSql Tc = new TableColumeToSql();
                Tc.Colume_Code = var.Colume_Code;
                Tc.Colume_DefaultValue = var.Colume_DefaultValue;
                Tc.Colume_Identity = var.Colume_Identity;
                Tc.Colume_IsKey = var.Colume_IsKey;
                Tc.Colume_IsNullAble = var.Colume_IsNullAble;
                Tc.Colume_Length = var.Colume_Length;
                Tc.Colume_Name = var.Colume_Name;
                Tc.Colume_Scale = var.Colume_Scale;
                Tc.Colume_Type = var.Colume_Type;
                Tc.SqlColume_DefaultValue = "";
                Tc.SqlColume_Type = "";
                ConvertToColumeToSql(Tc);
                NewList.Add(Tc);

            }
            return NewList;
        }


        private void ConvertToColumeToSql(TableColumeToSql tc)
        {
            string DefaultType = tc.Colume_Type;
            string DefaultValue = "''";
            switch (tc.Colume_Type)
            {
                case "bit":
                    DefaultValue = "0";
                    break;
                case "int":
                    DefaultValue = "0";
                    break;
                case "smallint":
                    DefaultValue = "0";
                    break;
                case "tinyint":
                    DefaultValue = "0";
                    break;
                case "numeric":
                    DefaultValue = "0";
                    DefaultType = string.Format("numeric ({0},{1})", tc.Colume_Length, tc.Colume_Scale);
                    break;
                case "decimal":
                    DefaultValue = "0";
                    DefaultType = string.Format("numeric ({0},{1})", tc.Colume_Length, tc.Colume_Scale);
                    break;
                case "money":
                    DefaultValue = "0";
                    break;
                case "smallmoney":
                    DefaultValue = "0";
                    break;
                case "float":
                    DefaultValue = "0";
                    break;
                case "real":
                    DefaultValue = "0";
                    break;
                case "datetime":
                    DefaultValue = "getdate";
                    break;
                case "smalldatetime":
                    DefaultValue = "getdate";
                    break;
                case "char":
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "varchar":
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "text":
                    break;
                case "nchar":
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "nvarchar":
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "ntext":
                    break;
                case "binary":
                    DefaultValue = "null";
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "varbinary":
                    DefaultValue = "null";
                    DefaultType = string.Format("{0}({1})", tc.Colume_Type, tc.Colume_Length);
                    break;
                case "image":
                    DefaultValue = "null";
                    break;
                case "bigint":
                    DefaultValue = "0";
                    break;
            }
            tc.SqlColume_Type = DefaultType;
            tc.SqlColume_DefaultValue = DefaultValue;
        }
        #endregion
    }
}
